import apiClient from '@/utils/request'

// 认证相关API
export const authApi = {
  login(credentials) {
    return apiClient.post('/login', credentials)
  },
  logout() {
    return apiClient.post('/logout')
  },
  getUserInfo() {
    return apiClient.get('/user/info')
  },
  updateNickname(data) {
    return apiClient.put('/user/nickname', data)
  }
}

// 分类相关API
export const categoryApi = {
  getLatest() {
    return apiClient.get('/categories/latest')
  },
  getCategories(params = {}) {
    return apiClient.get('/categories', { params })
  },
  getCategory(id) {
    return apiClient.get(`/categories/${id}`)
  },
  createCategory(data) {
    return apiClient.post('/categories', data)
  },
  updateCategory(id, data) {
    return apiClient.put(`/categories/${id}`, data)
  },
  deleteCategory(id) {
    return apiClient.delete(`/categories/${id}`)
  }
}

// 文章相关API
export const articleApi = {
  getArticle(id) {
    return apiClient.get(`/articles/${id}`)
  },
  getArticles(params = {}) {
    return apiClient.get('/articles', { params })
  },
  createArticle(data) {
    return apiClient.post('/articles', data)
  },
  updateArticle(id, data) {
    return apiClient.put(`/articles/${id}`, data)
  },
  deleteArticle(id) {
    return apiClient.delete(`/articles/${id}`)
  },
  batchDelete(ids) {
    return apiClient.post('/articles/batch-delete', { ids })
  }
}

// 标签相关API
export const tagApi = {
  getTags(params = {}) {
    return apiClient.get('/tags', { params })
  },
  getTag(id) {
    return apiClient.get(`/tags/${id}`)
  },
  createTag(data) {
    return apiClient.post('/tags', data)
  },
  updateTag(id, data) {
    return apiClient.put(`/tags/${id}`, data)
  },
  deleteTag(id) {
    return apiClient.delete(`/tags/${id}`)
  }
}

// 留言相关API
export const messageApi = {
  getMessages(params = {}) {
    return apiClient.get('/messages', { params })
  },
  replyMessage(id, data) {
    return apiClient.post(`/messages/${id}/reply`, data)
  },
  deleteMessage(id) {
    return apiClient.delete(`/messages/${id}`)
  },
  batchDelete(ids) {
    return apiClient.delete('/messages', { data: { ids } })
  }
}

// 相册相关API
export const photoApi = {
  getPhotos(params = {}) {
    return apiClient.get('photos', { params })
  },
  showPhoto(data) {
    return apiClient.post('photos/show', data)
  },
  createPhoto(data) {
    return apiClient.post('photos/store', data)
  },
  updatePhoto(data) {
    return apiClient.post('photos/update', data)
  },
  deletePhoto(data) {
    return apiClient.post('photos/destroy', data)
  },
  uploadPhoto(formData) {
    return apiClient.post('upload/photo', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}

// 统计相关API
export const statsApi = {
  getDashboardStats() {
    return apiClient.get('/stats/dashboard')
  },
  getArticleStats(params = {}) {
    return apiClient.get('/stats/articles', { params })
  },
  getCategoryStats() {
    return apiClient.get('/stats/categories')
  },
  getTagStats() {
    return apiClient.get('/stats/tags')
  },
  getMessageStats(params = {}) {
    return apiClient.get('/stats/messages', { params })
  },
  getUserStats(params = {}) {
    return apiClient.get('/stats/users', { params })
  },
  getPhotoStats(params = {}) {
    return apiClient.get('/stats/photos', { params })
  },
  getCommentStats(params = {}) {
    return apiClient.get('/stats/comments', { params })
  }
}

// 文件上传API
export const uploadApi = {
  uploadAvatar(file) {
    const formData = new FormData()
    formData.append('avatar', file)
    return apiClient.post('/upload/avatar', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },
  uploadCover(file) {
    const formData = new FormData()
    formData.append('cover', file)
    return apiClient.post('/upload/cover', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },
}